#include "formpermission.h"
#include "ui_formpermission.h"
#include <mysql.h>
#include <QHBoxLayout>
#include <QMessageBox>

FormPermission::FormPermission(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormPermission)
{
    ui->setupUi(this);
    this->show();
    this->setWindowTitle("用户权限设置");
    RefUSERS();
    RefPermission();
    ui->cmbUserType->clear();
    ui->cmbUserType->addItem("普通用户");
    ui->cmbUserType->addItem("超级用户");
    ui->cmbUserType->addItem("受限用户");

}

FormPermission::~FormPermission()
{
    delete ui;
}
void FormPermission::RefPermission() //初权限列表
{
    ui->treeWidgetPermission->clear();
    ui->treeWidgetPermission->setHeaderLabels(QStringList() << "用户权限" << "索引");
    ui->treeWidgetPermission->setColumnWidth(0,280);
    ui->treeWidgetPermission->setColumnWidth(1,50);
    QStringList tr1; //根节点
    QStringList child; //子节点

    QTreeWidgetItem *tree1;//根节点
    QTreeWidgetItem *tree11;//子节点

    tr1.clear();
    tr1 << "系统设置";
    child << "";
    tree1= new QTreeWidgetItem(ui->treeWidgetPermission, tr1);//根节点

    child.clear();
    child << "用户设置";
    child << "1";
    tree11 = new QTreeWidgetItem(tree1, child);  //子节点
    tree11->setCheckState(0, Qt::Unchecked);
    child.clear();
    child << "数据库设置";
    child << "2";
    tree11 = new QTreeWidgetItem(tree1, child);
    tree11->setCheckState(0, Qt::Unchecked);


    tr1.clear();
    tr1 << "会计账务";
    tree1 = new QTreeWidgetItem(ui->treeWidgetPermission, tr1);//根节点
    child.clear();
    child << "科目设置";
    child << "3";
    tree11 = new QTreeWidgetItem(tree1, child);  //子节点
    tree11->setCheckState(0, Qt::Unchecked);
    child.clear();
    child << "项目设置";
    child << "4";
    tree11 = new QTreeWidgetItem(tree1, child);  //子节点
    tree11->setCheckState(0, Qt::Unchecked);
    child.clear();
    child << "记账凭证";
    child << "5";
    tree11 = new QTreeWidgetItem(tree1, child);  //子节点
    tree11->setCheckState(0, Qt::Unchecked);
    child.clear();
    child << "凭证查询";
    child << "6";
    tree11 = new QTreeWidgetItem(tree1, child);  //子节点
    tree11->setCheckState(0, Qt::Unchecked);
    child.clear();
    child << "凭证导入";
    child << "7";
    tree11 = new QTreeWidgetItem(tree1, child);  //子节点
    tree11->setCheckState(0, Qt::Unchecked);
    child.clear();
    child << "财务系统参数";
    child << "8";
    tree11 = new QTreeWidgetItem(tree1, child);  //子节点
    tree11->setCheckState(0, Qt::Unchecked);
    child.clear();
    child << "通用报表";
    child << "9";
    tree11 = new QTreeWidgetItem(tree1, child);  //子节点
    tree11->setCheckState(0, Qt::Unchecked);
    child.clear();
    child << "创建会计年度";
    child << "10";
    tree11 = new QTreeWidgetItem(tree1, child);  //子节点
    tree11->setCheckState(0, Qt::Unchecked);
    child.clear();
    child << "财务凭证复核";
    child << "11";
    tree11 = new QTreeWidgetItem(tree1, child);  //子节点
    tree11->setCheckState(0, Qt::Unchecked);





}
void FormPermission::RefUSERS() //初始化用户列表
{
    ui->treeWidgetUSER->clear();
    ui->treeWidgetUSER->setHeaderLabels(QStringList() << "用户名" );
    ui->treeWidgetUSER->header()->setSectionResizeMode(QHeaderView::ResizeToContents); //QTreeWidget设置根据内容自适应宽度
    QString SQL="";
    SQL=QString("SELECT usertype,username FROM  users where type_id='%1' order by usertype,username").arg(mySQL::TypeId);
    QSqlQuery query(mySQL::cn); //查询
    query.exec(SQL);
    QString TypeID="";
    //这里用了循环，也就是说万一有多条记录只认最后一条记录。
    while(query.next()){
        QString str=query.value("usertype").toString().trimmed();
        if (TypeID!=str){
            QStringList child;
            child<<query.value("usertype").toString().trimmed();
            child<<"";
            QTreeWidgetItem * citem =new QTreeWidgetItem(child);
            ui->treeWidgetUSER->addTopLevelItem(citem);
            TypeID=str;
        }
        {
            QStringList child;
            child<<query.value("username").toString().trimmed();
            child<<query.value("usertype").toString().trimmed();
            QTreeWidgetItem * citem =new QTreeWidgetItem(child);
            QTreeWidgetItemIterator it(ui->treeWidgetUSER);
            while (*it) {
                 //(*it)->text(0);
                if ((*it)->text(0)==str){
                    (*it)->addChild(citem);
                }
                ++it;
            }
        }
    }
}

void FormPermission::on_treeWidgetUSER_itemClicked(QTreeWidgetItem *item, int column)
{
    {
        int i=column;
        i++;
        i--;
     }
    if (item->text(1)!=""){
        QString SQL="";
        SQL=QString("SELECT user_id,username,password,usertype,type_id,permission FROM users WHERE username='%1'").arg(item->text(0));
        SQL +=QString(" and type_id='%1'").arg(mySQL::TypeId);
        QSqlQuery query(mySQL::cn); //查询
        query.exec(SQL);
        //这里用了循环，也就是说万一有多条记录只认最后一条记录。
        while(query.next()){
            ui->txtUserName->setText(query.value("username").toString().trimmed());
            QString sPWS=query.value("password").toString();
            //sPWS 为BASE64编码 解码过程：
            QByteArray text = sPWS.toLocal8Bit();
            sPWS = text.fromBase64(text);
            ui->txtPassWord->setText(sPWS);
            ui->cmbUserType->setCurrentText(query.value("usertype").toString().trimmed());
            QString Permission=query.value("permission").toString();
            qDebug() << Permission;
            for (int i=0;i<50;i++){
                QString tmp=Permission.mid(i,1);
                //qDebug() << i << tmp;
                QTreeWidgetItemIterator it(ui->treeWidgetPermission);
                while (*it) {
                    if ((*it)->text(1)==QString("%1").arg(i+1)){
                        if (tmp=="1"){
                            (*it)->setCheckState(0,Qt::Checked);

                        }else{
                            (*it)->setCheckState(0,Qt::Unchecked);
                        }
                    }
                ++it;
                }
            }
        }
    }
}

void FormPermission::on_cmdReturn_clicked()
{

    this->close();
    //QMessageBox::warning(this,"警告",QX);
}

void FormPermission::on_cmdSave_clicked()
{
    //读取权限信息
    QString QX="";
    for  (int i=0;i<50;i++){
        QX=QString("%1%2").arg(QX).arg("1");
    }
    QTreeWidgetItemIterator it(ui->treeWidgetPermission);
    while (*it) {
         //(*it)->text(0);
        if ((*it)->checkState(0)!=Qt::Checked){
            if ((*it)->text(1)!=""){
                QString str="";
                str=QX.left((*it)->text(1).toInt()-1);
                str+="0";
                str+=QX.right(QX.length()-(*it)->text(1).toInt());
                QX=str;
                //qDebug() << QX << QX.length()<<(*it)->text(1);

            }
        }
        ++it;
    }
    //解析密码
    QString sPWS=ui->txtPassWord->text().trimmed();
    qDebug() << sPWS;
    //sPWS 为BASE64编码 解码过程：
    QByteArray text = sPWS.toLocal8Bit();
    sPWS =text.toBase64();

    //这里编写保存用户的代码
    //QString SQL="INSERT INTO users(user_id,username,password,usertype,type_id,permission`) VALUES (";
    QString SQL="";
    int User_ID=0;
    SQL=QString("SELECT user_id,username,password,usertype,type_id,permission FROM users WHERE username='%1'").arg(ui->txtUserName->text());
    SQL +=QString(" and type_id='%1'").arg(mySQL::TypeId);
    QSqlQuery query(mySQL::cn); //查询
    query.exec(SQL);
    while(query.next()){
        User_ID=query.value("user_id").toInt();
    }
    //qDebug() << "用户ID："<< User_ID;
    if(User_ID==0){
        SQL="INSERT INTO users(username,password,usertype,type_id,permission) VALUES (";
        SQL+=QString("'%1',").arg(ui->txtUserName->text().trimmed().replace("'","''"));
        SQL+=QString("'%1',").arg(sPWS.trimmed().replace("'","''"));
        SQL+=QString("'%1',").arg(ui->cmbUserType->currentText().trimmed().replace("'","''"));
        SQL+=QString("'%1',").arg(mySQL::TypeId.trimmed().replace("'","''"));
        SQL+=QString("'%1')").arg(QX.trimmed().replace("'","''"));
    }else{
        SQL="update users set ";
        SQL+=QString("username='%1',").arg(ui->txtUserName->text().trimmed().replace("'","''"));
        SQL+=QString("password='%1',").arg(sPWS.trimmed().replace("'","''"));
        SQL+=QString("usertype='%1',").arg(ui->cmbUserType->currentText().trimmed().replace("'","''"));
        SQL+=QString("type_id='%1',").arg(mySQL::TypeId.trimmed().replace("'","''"));
        SQL+=QString("permission='%1' ").arg(QX.trimmed().replace("'","''"));
        SQL+=QString("where user_id=%1 ").arg(User_ID);
    }
    //qDebug() << SQL;
    query.exec(SQL);
    RefUSERS();
    QMessageBox::information(this,"用户保存","保存成功！");
}
